package com.leoman.plz_p;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.leoman.plz_p.utils.authority.AuthorityContext;
import com.leoman.plz_p.utils.zxing.CaptureActivity;
import com.leoman.plz_p.views.QrFragment;
import com.leoman.plz_p.views.enterprise.LoginActivity;
import com.leoman.plz_p.views.group.GroupFragment;
import com.leoman.plz_p.views.homepage.HomePageFragment;
import com.leoman.plz_p.views.mine.MineFragment;
import com.leoman.plz_p.views.study.StudyFragment;

import butterknife.Bind;
import cn.com.corelib.base.BaseActivity;
import cn.com.corelib.base.BasePresenter;
import cn.com.corelib.utils.AppManager;
import cn.com.corelib.views.tab.InterceptedFragmentTabHost;
import cn.com.corelib.views.tab.TabChangeInterceptor;
import cn.com.corelib.views.tab.TabNavigator;

public class MainActivity extends BaseActivity implements TabNavigator.TabNavigatorContent{

    @Bind(R.id.real_tab_content)
    FrameLayout realTabContent;
    @Bind(R.id.tabhost)
    InterceptedFragmentTabHost tabHost;
    private boolean isBackPressed;
    private TabNavigator navigator = new TabNavigator();
    private String[] tabTags;
    private int[] imageResIds = new int[] {
            R.drawable.tab_homepage, R.drawable.tab_study,R.drawable.tab_qr,R.drawable.tab_group,R.drawable.tab_mine};

    public static Intent getLaunchIntent(Context context) {
        return new Intent(context, MainActivity.class);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setStatusBarColor(Color.parseColor("#45aeff"));
        tabTags = new String[] { "首页", "学习","","考试","我的" };
        navigator.setup(this, tabHost, this, getSupportFragmentManager(),
                R.id.real_tab_content);

        tabHost.setTabChangeInterceptor(new TabChangeInterceptor() {
            @Override
            public boolean canTab(String tabId) {
                if (TextUtils.isEmpty(tabId)){
                    return false;
                }else if (!AuthorityContext.getContext().isLoggedIn()&&(tabId.equals(tabTags[4])||tabId.equals(tabTags[1])||tabId.equals(tabTags[3]))){
                    return false;
                }else {
                    return true;
                }
            }

            @Override
            public void onTabIntercepted(String tabId) {
                if (TextUtils.isEmpty(tabId)) {
                    startActivity(new Intent(getViewContext(), CaptureActivity.class));
                }
                if (tabId.equals(tabTags[4])||tabId.equals(tabTags[1])||tabId.equals(tabTags[3])){
                    startActivity(new Intent(getViewContext(), LoginActivity.class));
                }
            }
        });
    }

    public void setCurrentPage(int page){
        tabHost.setCurrentTab(page);
    }



    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public View getTabView(int position) {
        View view = getLayoutInflater().inflate(R.layout.view_tab_content, null);
        ImageView iv = (ImageView) view.findViewById(R.id.iv_tab_icon);
        TextView tv = (TextView) view.findViewById(R.id.tv_tab_text);

        iv.setImageResource(imageResIds[position]);
        tv.setText(tabTags[position]);


        if (TextUtils.isEmpty(tabTags[position])){
            tv.setVisibility(View.GONE);
        }

        return view;
    }

    @Override
    public Bundle getArgs(int position) {
        return null;
    }

    @Override
    public Class[] getFragmentClasses() {
        return new Class[] { HomePageFragment.class, GroupFragment.class , QrFragment.class, StudyFragment.class, MineFragment.class};
    }

    @Override
    public String[] getTabTags() {
        return tabTags;
    }

    @Override
    public void onBackPressed() {
        doublePressBackToast();
    }

    /**
     * 双击返回退出
     */
    private void doublePressBackToast() {
        if (!isBackPressed) {
            isBackPressed = true;
            showToast("再次点击退出App");
        } else {
            AppManager.getAppManager().appExit();
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                isBackPressed = false;
            }
        }, 2000);
    }
}
